package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.IndicatorSystemDataMapper;
import com.ruoyi.system.mapper.IndicatorSystemMapper;
import com.ruoyi.system.service.IIndicatorSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-14
 */
@Service
public class IndicatorSystemServiceImpl implements IIndicatorSystemService
{
    @Autowired
    private IndicatorSystemMapper indicatorSystemMapper;

    @Autowired
    private IndicatorSystemDataMapper indicatorSystemDataMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public IndicatorSystem selectIndicatorSystemById(Long id)
    {
        return indicatorSystemMapper.selectIndicatorSystemById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param indicatorSystem 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<IndicatorSystem> selectIndicatorSystemList(IndicatorSystem indicatorSystem)
    {
        return indicatorSystemMapper.selectIndicatorSystemList(indicatorSystem);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param indicatorSystem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIndicatorSystem(IndicatorSystem indicatorSystem)
    {
        return indicatorSystemMapper.insertIndicatorSystem(indicatorSystem);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param indicatorSystem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIndicatorSystem(IndicatorSystem indicatorSystem)
    {
        return indicatorSystemMapper.updateIndicatorSystem(indicatorSystem);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorSystemByIds(Long[] ids)
    {
        return indicatorSystemMapper.deleteIndicatorSystemByIds(ids) & indicatorSystemDataMapper.deleteIndicatorSystemDataBySystemIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorSystemById(Long id)
    {
        return indicatorSystemMapper.deleteIndicatorSystemById(id) & indicatorSystemDataMapper.deleteIndicatorSystemDataBySystemId(id);
    }

    @Override
    public int creatIndicatorSystem(IndicatorSystemVo indicatorSystemvo)
    {
        IndicatorSystem indicatorSystem = indicatorSystemvo.getIndicatorSystem();
        indicatorSystem.setCreateTime(DateUtils.getNowDate());
        indicatorSystem.setCreateUser(SecurityUtils.getUsername());
        indicatorSystem.setSystemType(indicatorSystemvo.getSystemType());
        indicatorSystem.setCreateDeptId(SecurityUtils.getDeptId());
        indicatorSystemMapper.insertIndicatorSystem(indicatorSystem);
        Long id = indicatorSystem.getId();
        int cnt = 0;
        try{
            for(IndicatorVo vo : indicatorSystemvo.getIndicatorWeight())
            {
                IndicatorSystemData indicatorSystemData = new IndicatorSystemData();
                indicatorSystemData.setSystemId(id);
                indicatorSystemData.setIndicatorId(vo.getId());
                indicatorSystemData.setSubjectiveWeight(vo.getSubjectiveWeight());
                indicatorSystemData.setCreateTime(DateUtils.getNowDate());
                System.out.println(vo.getId()+","+vo.getSubjectiveWeight());
                indicatorSystemDataMapper.insertIndicatorSystemData(indicatorSystemData);
                cnt++;
            }
        }catch (Exception e){
            return cnt;
        }
        return cnt;
    }

    @Override
    public int countIndicatorSystem() {
        return indicatorSystemMapper.countIndicatorSystem();
    }

    @Override
    public List<IndicatorSystem> search(QueryVo queryVo) {
        return indicatorSystemMapper.selectIndicatorSystemListUseQueryParam(queryVo);
    }

}
